package Controller;

import model.Ghost;
import model.Map;
import model.Pack;
import view.Drawable;
import view.DrawingArea;
import view.GhostGraphics;
import view.MapGraphics;
import view.PackGraphics;

public class App {
	static final int tamX = 800;
	static final int tamY = 580;
	//static final int tamX = 980;
	//static final int tamY = 620;
	
	static final int tasrmFontCode = 10;
	static final int tamFontMenu = 10;
	static final boolean debugModeOn = true;
	
	static final String beginningClip = "music/pacman_beginning.wav";
	static final String deathClip = "music/pacman_beginning.wav";
	
	public static void main(String args[]){
		MyWindow window = new MyWindow("ventana",tamX ,tamY);
		DrawingArea area = new DrawingArea("sprites/level/bg.png",tamX, tamY,debugModeOn);
		Map map = createMap();
		
		Ghost red = new Ghost(410,275,map);
		Ghost purple = new Ghost(465,275,map);
		Ghost blue = new Ghost(515,275,map);
		Ghost orange = new Ghost(565,275,map);
		
		Pack pMan = new Pack(225,450,map);

		area.addObject(createPackGraphics(pMan));
		area.addObject(createMapGraphics(map));
		area.addObject(createGhostGraphics("red",red));
		area.addObject(createGhostGraphics("purple",purple));
		area.addObject(createGhostGraphics("blue",blue));
		area.addObject(createGhostGraphics("orange",orange));
		
		window.addComponent(area, 1, 1, 0, 0);

		game(window,pMan,red,purple,orange,blue);
	}
 
	public static void game(MyWindow window, Pack pMan, Ghost red, Ghost purple, Ghost orange, Ghost blue){
		view.Audio.playSound(beginningClip); 
		while(true){
			pMan.move();
			red.move();
			orange.move();
			purple.move();
			blue.move();
			
			if(pMan.colidesWith(red) || pMan.colidesWith(blue) || pMan.colidesWith(orange) || pMan.colidesWith(purple) ){
				pMan.die();
				view.Audio.playSound(deathClip); 
			}
			window.redraw();
			try {
				Thread.sleep(80);
			} catch (InterruptedException e) {e.printStackTrace();}
		}
	}
	
	// creates the paths for the packman image list
	private static PackGraphics createPackGraphics(Pack pMan){
		String imgs[] = { getPath("pack","up1"),getPath("pack","up2"),getPath("pack","up3"),getPath("pack","up2"),
						  getPath("pack","down1"),getPath("pack","down2"),getPath("pack","down3"),getPath("pack","down2"),
						  getPath("pack","left1"),getPath("pack","left2"),getPath("pack","left3"),getPath("pack","left2"),
						  getPath("pack","right1"),getPath("pack","right2"),getPath("pack","right3"),getPath("pack","right2"),
						  getPath("pack","dead1"),getPath("pack","dead2"),getPath("pack","dead3"),getPath("pack","dead4")};
		return new PackGraphics(imgs,pMan,debugModeOn);
	}

	// creates the paths for a ghost image list
	private static GhostGraphics createGhostGraphics(String color,Ghost ghost){
		String imgs[] = {getPath(color,"up"),getPath(color,"down"),getPath(color,"left"),getPath(color,"right"),getPath(color,"dead")};
		return new GhostGraphics(imgs,ghost,debugModeOn);
	}
	
	// creates the path for the desired image
	private static String getPath(String color,String state){
		return new String("sprites/"+color+"/"+color+"_"+state+".png");
	}
	
	private static Drawable createMapGraphics(Map map) {
		return new MapGraphics(map,debugModeOn);
	}
	
	// creates and retrurns a map
	private static Map createMap() {
		Map map = new Map();
		
		// up
		map.addWall(20,15,950,15);
		map.addWall(20,15,20,180);
		map.addWall(950,15,950,180);

		// middle up
		map.addWall(465,15,465,90);
		map.addWall(510,15,510,90);
		map.addWall(465,90,510,90);
		
		//left
		map.addWall(20,180,190,180);
		map.addWall(190,180,190,250);
		map.addWall(0,250,190,250);
	
		map.addWall(0,290,190,290);
		map.addWall(190,290,190,360);
		map.addWall(0,360,190,360);
		

		//right
		map.addWall(950,180,780,180);
		map.addWall(780,180,780,250);
		map.addWall(780,250,980,250);
	
		map.addWall(780,290,980,290);
		map.addWall(780,290,780,360);
		map.addWall(980,360,780,360);
		
		//stuff
		map.addWall(90,50,90,85);
		map.addWall(90,85,190,85);
		map.addWall(190,85,190,50);
		map.addWall(190,50,90,50);
				
		map.addWall(780,50,780,85);
		map.addWall(780,85,880,85);
		map.addWall(880,85,880,50);
		map.addWall(880,50,780,50);
		
		map.addWall(265,50,265,90);
		map.addWall(265,90,400,90);
		map.addWall(400,90,400,50);
		map.addWall(400,50,265,50);		
		
		map.addWall(575,50,575,90);
		map.addWall(575,90,710,90);		
		map.addWall(710,90,710,50);
		map.addWall(710,50,575,50);	
		
		map.addWall(20,460,90,460);	
		map.addWall(90,460,90,475);
		map.addWall(90,475,20,475);

		map.addWall(950,460,890,460);	
		map.addWall(890,460,890,475);
		map.addWall(890,475,950,475);
		
		map.addWall(260,290,300,290);	
		map.addWall(300,290,300,360);
		map.addWall(300,360,260,360);	
		map.addWall(260,360,260,290);
		
		map.addWall(680,295,710,295);	
		map.addWall(710,295,710,360);
		map.addWall(710,360,680,360);	
		map.addWall(680,360,680,295);
	
		map.addWall(90,125,190,125);	
		map.addWall(190,125,190,140);
		map.addWall(190,140,90,140);	
		map.addWall(90,140,90,125);

		map.addWall(780,400,885,400);	
		map.addWall(885,400,885,420);
		map.addWall(885,420,820,420);	
		map.addWall(820,420,820,470);
		map.addWall(820,470,780,470);	
		map.addWall(780,470,780,400);
		
		map.addWall(365,125,600,125);	
		map.addWall(600,125,600,140);
		map.addWall(600,140,510,140);
		map.addWall(510,140,510,195);
		map.addWall(510,195,470,195);
		map.addWall(470,195,470,140);	
		map.addWall(470,140,365,140);
		map.addWall(365,140,365,125);	
		map.addWall(365,125,365,125);

		map.addWall(365,460,600,460);	
		map.addWall(600,460,600,475);
		map.addWall(600,475,510,475);
		map.addWall(510,475,510,530);
		map.addWall(510,530,470,530);
		map.addWall(470,530,470,475);	
		map.addWall(470,475,365,475);
		map.addWall(365,475,365,460);	
		map.addWall(365,460,365,460);

		map.addWall(365,350,600,350);	
		map.addWall(600,350,600,365);
		map.addWall(600,365,510,365);
		map.addWall(510,365,510,420);
		map.addWall(510,420,470,420);
		map.addWall(470,420,470,365);	
		map.addWall(470,365,365,365);
		map.addWall(365,365,365,350);	
		map.addWall(365,350,365,350);		

		map.addWall(575,510,675,510);
		map.addWall(675,510,675,510);	
		map.addWall(675,510,675,460);
		map.addWall(675,460,715,460);
		map.addWall(715,460,715,510);
		map.addWall(715,510,880,510);	
		map.addWall(880,510,880,530);
		map.addWall(880,530,575,530);
		map.addWall(575,530,575,510);		

		map.addWall(260,125,260,255);	
		map.addWall(260,255,300,255);
		map.addWall(300,255,300,200);
		map.addWall(300,200,400,200);	
		map.addWall(400,200,400,180);	
		map.addWall(400,180,300,180);
		map.addWall(300,180,300,125);
		map.addWall(300,125,260,125);	

		map.addWall(680,130,710,130);	
		map.addWall(710,130,710,250);
		map.addWall(710,250,680,250);
		map.addWall(680,250,680,200);	
		map.addWall(680,200,570,200);	
		map.addWall(570,200,570,180);
		map.addWall(570,180,680,180);
		map.addWall(680,180,680,130);
		
		map.addWall(450,240,375,240);	
		map.addWall(375,240,375,300);	
		map.addWall(375,300,600,300);
		map.addWall(600,300,600,240);
		map.addWall(600,240,520,240);
		
		map.addWall(90,510,260,510);
		map.addWall(260,510,260,510);	
		map.addWall(260,510,260,460);
		map.addWall(260,460,300,460);
		map.addWall(300,460,300,510);
		map.addWall(300,510,400,510);	
		map.addWall(400,510,400,530);
		map.addWall(400,530,90,530);
		map.addWall(90,530,90,510);
		
		map.addWall(90,400,190,400);
		map.addWall(190,400,190,475);	
		map.addWall(190,475,155,475);
		map.addWall(155,475,155,420);
		map.addWall(155,420,90,420);	
		map.addWall(90,420,90,400);		
		
		map.addWall(260,400,400,400);	
		map.addWall(400,400,400,420);
		map.addWall(400,420,260,420);	
		map.addWall(260,420,260,400);		
		
		map.addWall(575,400,710,400);	
		map.addWall(710,400,710,420);
		map.addWall(710,420,575,420);	
		map.addWall(575,420,575,400);		
		
		map.addWall(780,127,880,127);	
		map.addWall(880,127,880,140);
		map.addWall(880,140,780,140);	
		map.addWall(780,140,780,127);	
		
		map.addWall(20,565,20,365);
		map.addWall(950,565,950,365);
		map.addWall(20,565,950,565);
		return map;
	}
	
}
